unit Daftar_Desa;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids,ZAbstractDataset, ZDataset, ZConnection,
  ExtCtrls, StdCtrls, DB, ZAbstractRODataset, ZAbstractTable, Setting;

type
  wilayah = class(TObject)
    kode:string;
    kota:string;
    kec:string;
    kel:string;
    path:string;
  end;
  TTDaftar_Desa = class(TForm)
    StringGrid: TStringGrid;
    Label1: TLabel;
    ZConnection1: TZConnection;
    ZTable1: TZTable;
    btnHapus: TButton;
    lblKec: TLabel;
    lblDesa: TLabel;
    lblKota: TLabel;
    ZQuery1: TZQuery;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure StringGridClick(Sender: TObject);
    procedure btnHapusClick(Sender: TObject);

  private
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    procedure refreshData;
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent);overload;
  end;

var
  TDaftar_Desa: TTDaftar_Desa;
  listwilayah:array of wilayah;

implementation

{$R *.dfm}

procedure TTDaftar_Desa.btnHapusClick(Sender: TObject);
var
  buttonSelected : Integer;
  que:string;
begin
  buttonSelected := MessageDlg('Apakah anda yakin akan menghapus desa ini dari database?',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
    with ZQuery1 do begin
      //hapus tabel
      Active:=false;
      sql.Clear;
      que:='DROP TABLE '+listwilayah[StringGrid.Row-1].kode;
      ShowMessage(que);
      sql.Text:=que;
      ExecSQL;
      //HAPUS penyimpan
      Active:=false;
      sql.Clear;
      que:='DELETE FROM `penyimpan` WHERE NAMA='+QuotedStr(listwilayah[StringGrid.Row-1].kode);
      ShowMessage(que);
      sql.Text:=que;
      ExecSQL;
    end;
    ShowMessage('Desa '+listwilayah[StringGrid.Row-1].kel+' sudah dihapus');
    refreshData;
  end;
end;

constructor TTDaftar_Desa.Create(AOwner: TComponent);
begin
    //Copy(sLine,0,(Length(sLine)-1))
    inherited Create(AOwner);

end;


procedure TTDaftar_Desa.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TTDaftar_Desa.FormCreate(Sender: TObject);
var
    i:integer;
    setting:TSetting_Form;
begin
    WindowState:=wsNormal;
    setting:=TSetting_Form.Create;
    ZConnection1.HostName:=setting.host;
    ZConnection1.Protocol:=setting.protocol;
    ZConnection1.Database:=setting.database;
    ZConnection1.User:=setting.user;
    ZConnection1.Password:=setting.password;
    ZConnection1.Connected:=true;
    ztable1.Connection:=ZConnection1;
    ZQuery1.Connection:=ZConnection1;
    //
    refreshData;
end;

procedure TTDaftar_Desa.StringGridClick(Sender: TObject);
begin
  if (stringgrid.Cells[0,1]<>'') then begin
    if (StringGrid.Row>0) then begin
      btnHapus.Enabled:=True;
      lblKota.Caption:='Kota : '+listwilayah[StringGrid.Row-1].kota;
      lblKec.Caption:='Kec : '+listwilayah[StringGrid.Row-1].kec;
      lblDesa.Caption:='Desa : '+listwilayah[StringGrid.Row-1].kel;
    end;
  end;
end;

procedure TTDaftar_Desa.refreshData();
var
  temp:wilayah;
  i:integer;
  //dictkota, dictkec, dictkel:TDictionary<String,String>;
begin
  with Ztable1 do
    begin
      active := false;
      tablename:='penyimpan';
      active:=true;
      first;
    end;
    StringGrid.RowCount:=ztable1.RecordCount+1;
    stringgrid.ColCount:=5;
    SetLength(listwilayah, ztable1.RecordCount);

    i:=0;
    with ztable1 do
    begin
      first;
      while (not eof) do
      begin
//        stringgrid.cells[0,i]:=inttostr(i);
//        stringgrid.cells[1,i]:=FieldByName('NAMA').AsString;
        temp:=wilayah.Create;
        temp.kode:=FieldByName('NAMA').AsString;
        temp.kota:='';
        temp.kec:='';
        temp.kel:='';
        temp.path:=FieldByName('UPLOAD').AsString;
        listwilayah[i] := temp;
        next;
        inc(i);
      end;
    end;
//    dictkota:= tdictionary<string,string>.Create();
    with ztable1 do
    begin
      active:=false;
      tablename:='kabkot';
      active:=true;
      first;
      while (not eof) do
      begin
//        dictkota.Add(FieldByName('KODE').AsString, FieldByName('TAMPIL').AsString);
        next;
      end;
    end;

  //  dictkec:= tdictionary<string,string>.Create();
    with ztable1 do
    begin
      active:=false;
      tablename:='kecamatan';
      active:=true;
      first;
      while (not eof) do
      begin
//        dictkec.Add(FieldByName('KODE_KECAMATAN').AsString, FieldByName('TAMPIL').asstring);
        next;
      end;
    end;

//    dictkel:= tdictionary<string,string>.Create();
    with ztable1 do
    begin
      active:=false;
      tablename:='desa';
      active:=true;
      first;
      while (not eof) do
      begin
//        dictkel.Add(FieldByName('KODE_DESA').AsString, FieldByName('TAMPIL').asstring);
        next;
      end;
    end;

    stringgrid.cells[0,0]:='No';
    stringgrid.cells[1,0]:='Kota';
    stringgrid.cells[2,0]:='Kecamatan';
    stringgrid.cells[3,0]:='Desa';
    stringgrid.cells[4,0]:='PathFile';
    {stringgrid.ColWidths[1]:=150;
    stringgrid.ColWidths[2]:=150;
    stringgrid.ColWidths[3]:=150;}
    for I := 1 to Length(listwilayah) do
    begin
{      if (dictkota.TryGetValue(copy(listwilayah[i-1].kode, 9,2), listwilayah[i-1].kota) <> True) then
      begin
        listwilayah[i-1].kota:='';
      end;
      if (dictkec.TryGetValue(copy(listwilayah[i-1].kode, 9,5), listwilayah[i-1].kec) <> True) then
      begin
        listwilayah[i-1].kec:='';
      end;
      if (dictkel.TryGetValue(copy(listwilayah[i-1].kode, 9,8), listwilayah[i-1].kel) <> True) then
      begin
        listwilayah[i-1].kel:='';
      end;}
      stringgrid.Cells[0,I]:=inttostr(i);
      stringgrid.Cells[1,I]:=listwilayah[i-1].kota;
      stringgrid.Cells[2,I]:=listwilayah[i-1].kec;
      stringgrid.Cells[3,I]:=listwilayah[i-1].kel;
      stringgrid.Cells[4,I]:=listwilayah[i-1].path;
    end;
    for i := 0 to stringgrid.ColCount - 1 do
      AutoSizeCol(stringgrid, i);
    btnHapus.Enabled:=false;
    lblKota.Caption:='Kota : -';
    lblKec.Caption:='Kec : -';
    lblDesa.Caption:='Desa : -';
end;

procedure TTDaftar_Desa.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

end.
